package cn.frh.pet.service.impl;

import cn.frh.basic.service.impl.BaseServiceImpl;
import cn.frh.basic.util.CodeGenerateUtils;
import cn.frh.basic.util.DistanceUtil;
import cn.frh.basic.util.PageList;
import cn.frh.basic.util.Point;
import cn.frh.order.domain.PetAcquisitionOrder;
import cn.frh.order.mapper.PetAcquisitionOrderMapper;
import cn.frh.org.domain.Employee;
import cn.frh.org.domain.Shop;
import cn.frh.org.mapper.EmployeeMapper;
import cn.frh.org.mapper.ShopMapper;
import cn.frh.pet.domain.Pet;
import cn.frh.pet.domain.SearchMasterMsg;
import cn.frh.pet.mapper.SearchMasterMsgMapper;
import cn.frh.pet.query.SearchMasterMsgQuery;
import cn.frh.pet.service.IPetService;
import cn.frh.pet.service.ISearchMasterMsgService;
import cn.frh.user.domain.Logininfo;
import cn.frh.user.domain.User;
import cn.frh.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;

    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;


    /**
     * 发布寻主消息业务
     * @param searchMasterMsg
     * @param logininfo
     */
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Logininfo logininfo) {
        //绑定当前登录人
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        searchMasterMsg.setUser_id(user.getId());
        //1消息要分配给最近店铺
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //1.1 获取最近店铺
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        //设置最近店铺id
        searchMasterMsg.setShop_id(nearestShop.getId());
        //保存寻主消息
        searchMasterMsgMapper.save(searchMasterMsg);
    }

    /**
     * 用户查看自己发布的寻主消息
     * @param query
     * @param logininfo
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, Logininfo logininfo) {
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        if (user!=null){
            query.setUserId(user.getId());
        }
        return super.queryPage(query);
    }

    /**
     * 查看待处理的寻主消息
     * @param query
     * @param logininfo
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Logininfo logininfo) {
        query.setState(0);//查看待处理寻主消息
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if (employee.getShop_id()!=null){//如果店铺id不为null就说明是店铺自己查看待处理的寻主消息
            query.setShopId(employee.getShop_id());//查看某一个店铺的寻主消息
        }
        return super.queryPage(query);
    }

    /**
     * 查看已处理的寻主消息
     * @param query
     * @param logininfo
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Logininfo logininfo) {
        query.setState(1);//查看已处理寻主消息
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if (employee.getShop_id()!=null){//如果店铺id不为null就说明是店铺自己查看待处理的寻主消息
            query.setShopId(employee.getShop_id());//查看某一个店铺的寻主消息
        }
        return super.queryPage(query);
    }

    @Override
    public void handle(Pet pet, Logininfo currentLogininfo) {
        //修改状态为已处理
        searchMasterMsgMapper.updateState2ProcessedById(pet.getSearch_master_msg_id());
        //生成宠物信息和宠物详情
        petService.add(pet);
        //生成收购订单
        //收购的哪个宠物
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        Employee employee = employeeMapper.loadByLogininfoId(currentLogininfo.getId());
        PetAcquisitionOrder acquisitionOrder = pet2order(pet, searchMasterMsg,employee.getId());
        petAcquisitionOrderMapper.save(acquisitionOrder);
    }


    /**
     * 创建收购订单
     * @param pet
     * @param adopt
     * @return
     */
    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        //创建订单唯一标识
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}
